<template>
  <audit-trade :id="id" :baseInfoProp="params" tradeType=2 widthPercent="25%" :goodsInfoPropParams="goodsAdditionalColumns"
               :tradeInfoPropParams="tradeAdditionalColumns" @audit-trade="auditBuyBack"></audit-trade>
</template>
<script type="text/ecmascript-6">
  import AuditTrade from '@/views/wms/trade/buy-back/auditTrade';
  import mValidate from 'utils/common-method/validate/index';
  export default {
    props: {
      id: [String, Number]
    },
    components: {AuditTrade},
    data() {
      return {
        params: [
          {
            label: '出售申请编号',
            labelIcon: ['icon maliiconfont maliicon-img_details_sell'],
            delimiter: ' ',
            key: 'sellArNo'
          }
        ],
        auditForm: {
          id: '', // 主键id
          auditType: '', // 审核类型(1:通过 2:不通过)
          arType: 1, // 交易类型(1:出售 2:回购)
          applyDesc: '', // 审核说明 varchar(200)
          repurchaseDate: '', // 回购日期 - 回购填写
          repurchaseLastDate: '', // 最晚回购日期 - 出售填写
          sellDate: '', // 出售日期 - 出售填写
          version: '', // 版本号
          wmsArAuditGoodsDtoList: '' // 审核出售/回购-货物信息
        },
        // 回购额外货物信息列
        goodsAdditionalColumns: [
          {
            label: '回购数量',
            prop: 'repurchaseNum'
          },
          {
            label: '参考回购重量（kg）',
            prop: 'refTotalWeight'
          }
        ],
        // 回购交易信息列数据
        tradeAdditionalColumns: [
          {
            label: '生产日期',
            prop: 'productDate'
          },
          {
            label: '回购数量',
            prop: 'repurchaseNum'
          },
          {
            label: '参考回购重量（kg）',
            prop: 'refTotalWeight'
          },
          {
            label: '抄码重量（kg）',
            prop: 'scanWeight'
          },
          {
            label: '交易金额（元）',
            prop: 'transactionAmount'
          }
        ]
      };
    },
    methods: {
      // 审核回购
      auditBuyBack(data) {
        let verify = data.auditType === 1;
        // 判断是否没有选择出售日期
        data.repurchaseDate = data.tradeDate;
        if (verify && mValidate.stringIsNullOrEmpty(data.repurchaseDate)) {
          this.errorTip('回购日期不可为空');
          return;
        }

        let arrayIsNullOrEmpty = mValidate.arrayIsNullOrEmpty(data.wmsArAuditGoodsDtoList); // 判断列表是否null
        let goodsLen = !arrayIsNullOrEmpty ? data.wmsArAuditGoodsDtoList.length : 0;
        let pass = !arrayIsNullOrEmpty;
        // 校验每种货物的交易金额
        for (let i = 0; i < goodsLen; i++) {
          if (!data.wmsArAuditGoodsDtoList[i].transactionAmount || data.wmsArAuditGoodsDtoList[i].transactionAmount <= 0) {
            pass = false;
            break;
          }
        }
        if (verify && !pass) {
          this.errorTip('每种货物的交易金额不可为空');
          return;
        }
        let message = data.auditType === 1 ? '确定审核通过吗？' : '确定审核不通过吗？';
        // 弹出确认对话框
        this.confirmModel(message, '/maliwms/ar/auditAr', data, (response) => {
          if (response.code === 200) {
            this.successTip('提交成功', 3000);
            this.$emit('closeRefresh');
          } else {
            this.errorTip(response.message, 3000);
          }
        });
      }
    }
  };
</script>
